import { defineStore } from "pinia";

import {
  getPageListData,
  editPageData,
  deletePageData,
  createPageData,
} from "../service/api/system";
import { ElMessage } from "element-plus";
const useSystemStore = defineStore("system", {
  state: () => {
    return {
      usersList: [],
      usersCount: 0,
      menuList: [],
      menuCount: 0,
      departmentList: [],
      departmentCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
    };
  },
  actions: {
    async getPageListAction(payload) {
      const pageName = payload.pageName;
      const pageUrl = `/${pageName}/list`;

      const pageResult = await getPageListData(pageUrl, payload.queryInfo);
      const { list, totalCount } = pageResult.data;

      this[`${payload.pageName}List`] = list;
      this[`${payload.pageName}Count`] = totalCount;
      // switch (payload.pageName) {
      //   case "users":
      //     this.usersList = list;
      //     this.usersCount = totalCount;
      //     break;
      //   case "menu":
      //     this.menuList = list;
      //     this.menuCount = totalCount;
      //     break;
      //   case "department":
      //     this.departmentList = list;
      //     this.departmentCount = totalCount;
      //     break;
      //   case "role":
      //     this.roleList = list;
      //     this.roleCount = totalCount;
      //     break;
      //   case "goods":
      //     this.goodsList = list;
      //     this.goodsCount = totalCount;
      //     break;
      // }
    },
    async deletePageData(payload) {
      const { pageName, id } = payload;
      const pageUrl = `/${pageName}/${id}`;

      const del = await deletePageData(pageUrl);
      if (del.code == 0) {
        ElMessage({
          message: del.data,
          type: "success",
          plain: true,
        });
        await this.getPageListAction({
          pageName,
          queryInfo: {
            offset: 0,
            size: 10,
          },
        });
      } else {
        ElMessage({
          message: del.data,
          type: "error",
          plain: true,
        });
      }
    },
    async createPageData(payload) {
      const { pageName, newData } = payload;
      const pageUrl = `/${pageName}`;
      const { code, data } = await createPageData(pageUrl, newData);
      if (code === 0) {
        await this.getPageListAction({
          pageName,
          queryInfo: {
            offset: 0,
            size: 10,
          },
        });
        ElMessage({ message: data, type: "success" });
      }
    },
    async editPageData(payload) {
      const { pageName, editData, id } = payload;
      const pageUrl = `/${pageName}/${id}`;

      const { code, data } = await editPageData(pageUrl, editData);
      if (code === 0) {
        await this.getPageListAction({
          pageName,
          queryInfo: {
            offset: 0,
            size: 10,
          },
        });
        ElMessage({ message: data, type: "success" });
      }
    },
  },
  getters: {},
});

export default useSystemStore;
